package mips;

public class Fibonacci {
	
	public static void writeFibToMemory() {

		// 16 instructions starting at 0xA0000000
		// this is the assembled version of start.s
		n64.MemoryManager.RDRAM[0] = 0x27bdffe0;
		n64.MemoryManager.RDRAM[1] = 0xafbf0014;
		n64.MemoryManager.RDRAM[2] = 0xafbe0010;
		n64.MemoryManager.RDRAM[3] = 0x27be0002;
		n64.MemoryManager.RDRAM[4] = 0x24040000; // loads $a0 for getFibonacci($a0)
		n64.MemoryManager.RDRAM[5] = 0x0c000010;
		n64.MemoryManager.RDRAM[6] = 0x00000020;
		n64.MemoryManager.RDRAM[7] = 0x8fbf0014;
		n64.MemoryManager.RDRAM[8] = 0x8fbe0010;
		n64.MemoryManager.RDRAM[9] = 0x27bd0020;
		n64.MemoryManager.RDRAM[10] = 0x0800000a;
		n64.MemoryManager.RDRAM[11] = 0x00000020;
		n64.MemoryManager.RDRAM[12] = 0x00000020;
		n64.MemoryManager.RDRAM[13] = 0x00000020;
		n64.MemoryManager.RDRAM[14] = 0x00000020;
		n64.MemoryManager.RDRAM[15] = 0x00000020;
		
		// 44 instructions starts at 0xA0000040
		// this is the assembled version of fib.s
		n64.MemoryManager.RDRAM[16] = 0x27bdffd8;
		n64.MemoryManager.RDRAM[17] = 0xafbf0020;
		n64.MemoryManager.RDRAM[18] = 0xafbe001c;
		n64.MemoryManager.RDRAM[19] = 0xafb00018;
		n64.MemoryManager.RDRAM[20] = 0x03a0f020;
		n64.MemoryManager.RDRAM[21] = 0xafc40028;
		n64.MemoryManager.RDRAM[22] = 0x8fc20028;
		n64.MemoryManager.RDRAM[23] = 0x00000020;
		n64.MemoryManager.RDRAM[24] = 0x14400004;
		n64.MemoryManager.RDRAM[25] = 0x00000020;
		n64.MemoryManager.RDRAM[26] = 0xafc00010;
		n64.MemoryManager.RDRAM[27] = 0x08000034;
		n64.MemoryManager.RDRAM[28] = 0x00000020;
		n64.MemoryManager.RDRAM[29] = 0x8fc30028;
		n64.MemoryManager.RDRAM[30] = 0x24020001;
		n64.MemoryManager.RDRAM[31] = 0x14620005;
		n64.MemoryManager.RDRAM[32] = 0x00000020;
		n64.MemoryManager.RDRAM[33] = 0x24020001;
		n64.MemoryManager.RDRAM[34] = 0xafc20010;
		n64.MemoryManager.RDRAM[35] = 0x08000034;
		n64.MemoryManager.RDRAM[36] = 0x00000020;
		n64.MemoryManager.RDRAM[37] = 0x8fc20028;
		n64.MemoryManager.RDRAM[38] = 0x00000020;
		n64.MemoryManager.RDRAM[39] = 0x2442ffff;
		n64.MemoryManager.RDRAM[40] = 0x00402020;
		n64.MemoryManager.RDRAM[41] = 0x0c000010;
		n64.MemoryManager.RDRAM[42] = 0x00000020;
		n64.MemoryManager.RDRAM[43] = 0x00408020;
		n64.MemoryManager.RDRAM[44] = 0x8fc20028;
		n64.MemoryManager.RDRAM[45] = 0x00000020;
		n64.MemoryManager.RDRAM[46] = 0x2442fffe;
		n64.MemoryManager.RDRAM[47] = 0x00402020;
		n64.MemoryManager.RDRAM[48] = 0x0c000010;
		n64.MemoryManager.RDRAM[49] = 0x00000020;
		n64.MemoryManager.RDRAM[50] = 0x02028021;
		n64.MemoryManager.RDRAM[51] = 0xafd00010;
		n64.MemoryManager.RDRAM[52] = 0x8fc20010;
		n64.MemoryManager.RDRAM[53] = 0x03c0e820;
		n64.MemoryManager.RDRAM[54] = 0x8fbf0020;
		n64.MemoryManager.RDRAM[55] = 0x8fbe001c;
		n64.MemoryManager.RDRAM[56] = 0x8fb00018;
		n64.MemoryManager.RDRAM[57] = 0x27bd0028;
		n64.MemoryManager.RDRAM[58] = 0x03e00008;
		n64.MemoryManager.RDRAM[59] = 0x00000020; 	
	}
	
	/**
	 *	int getFibonacci(int num) {
	 *
	 *		if (num == 0) {
	 *
	 *			return 0;
	 *		} 
	 *		else if (num == 1) {
	 *
	 *			return 1;
	 *		} 
	 *		else {
	 *
	 *			return getFibonacci(num - 1) + getFibonacci(num - 2);
	 *		}
	 *	}
	 *
	 *	assembled with mips-linux-gcc for MIPS 2 ISA
	 *
	 *	#getFibonacci: 
	 *	=80000040: addiu $sp,$sp,-40 # $29 <- $29 + (-40); 1
	 *	=00100111101111011111111111011000
	 *	=0x27BDFFD8
	 * 
	 *	=80000044 sw $31,32($sp) # memory($29 + 32) <- $31; 2
	 *	=101011 11101 11111 0000000000100000
	 * 	=0xAFBF0020
	 * 
	 *	=80000048 sw $fp,28($sp) # memory($29 + 28) <- $30; 3
	 *	=101011 11101 11110 0000000000011100
	 *	=0xAFBE001C
	 * 
	 *	=8000004C sw $16,24($sp) # memory($29 + 24) <- $16; 4
	 *	=101011 11101 10000 0000000000011000
	 *	=0xAFB00018
	 * 
	 *	=80000050 add $fp,$sp,$0 # $30 <- $29 + $0; 5
	 *	=000000 11101 00000 11110 00000100000 
	 *	=0x03A0F020
	 * 
	 *	=80000054 sw $4,40($fp) # memory($30 + 40) <- $4 [$4 == arg0 (n)]; 6
	 *	=101011 11110 00100 0000000000101000 
	 *	=0xAFC40028
	 * 
	 *	=80000058 lw $2,40($fp) # $2 <- memory($30 + 40); 7
	 *	=100011 11110 00010 00000 00000101000
	 *	=0x8FC20028
	 * 
	 *	=8000005C nop # nop; 8
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=80000060 bne $2,$0,16 # if ($2 != 0) goto $L2; 9
	 *	=000101 00010 00000 00000000 00000100
	 *	=0x14400004
	 * 
	 *	=80000064 nop # nop - delay slot; 10
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=80000068 sw $0,16($fp) # memory($fp + 16) = 0; 11
	 *	=101011 11110000000000000000010000
	 *	=0xAFC00010
	 * 
	 *	=8000006C j 800000D0 # goto $L4; 12
	 *	=00001000000000000000000011010000
	 *	=0x080000D0
	 * 
	 *	=80000070 nop # nop - delay slot; 13
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=#$L2: 
	 *	=80000074: lw $3,40($fp) # $3 <- memory($fp + 40); 14
	 *	=100011 11110 00011 0000000000101000
	 *	=0x8FC30028
	 * 
	 *	=80000078 li $2,1 # $2 <- $0 + 1 [addiu $2,$0,1]; 15
	 *	=001001 00000 00010 0000000000000001
	 *	=0x24020001
	 * 
	 *	=8000007C bne $3,$2,5 # if($3 != $2) goto $L5; 16
	 *  =000101 00011 00010 00000000 00000101
	 *  =0x14620005
	 * 
	 *	=80000080 nop # nop - delay slot; 17
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 *
	 *	=80000084 li $2,1 # $2 <- $0 + 1 [addiu $2,$0,1]; 18
	 *	=001001 00000 00010 0000000000000001
	 *	=0x24020001
	 * 
	 *	=80000088 sw $2,16($fp) # memory($fp + 16) <- $2; 19
	 *	=101011 11110 00010 00000000 00010000
	 * 	=0xAFC20010
	 * 
	 *	=8000008C j 800000D0 # goto $L4; 20
	 *	=00001000000000000000000011010000
	 *	=0x080000D0
	 * 
	 *	=80000090 nop # nop - delay slot; 22
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=#$L5: 
	 *	=80000094 lw $2,40($fp) # $2 <- memory($fp + 40); 23
	 *	=100011 11110 00010 00000 00000101000
	 *	=0x8FC20028
	 * 
	 *	=80000098 nop # nop
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=8000009C addiu $2,$2,-1 # $2 <- $2 + -1
	 *	=001001 00010 00010 1111111111111111
	 *	=0x2442FFFF
	 * 
	 *	=800000A0 move $4,$2 # $4 <- $2 + 0 [add $4,$2,$0]
	 *	=00000000010000000010000000100000
	 *	=0x00402020
	 * 
	 *	=800000A4 jal 80000040 # link and goto getFibonacci
	 *	=00001100000000000000000001000000
	 *	=0x0C000040
	 * 
	 *	=800000A8 nop # nop - delay slot
	 *	=00000000000000000000000000100000
	 *	=0x00000020
	 * 
	 *	=800000AC move $16,$2 # $16 <- $2 + $0 [add $16,$2,$0]
	 *	=00000000010000001000000000100000
	 *	=0x00408020
	 * 
	 *	=800000B0 lw $2,40($fp) # $2 <- memory($fp + 40)
	 *	=100011 11110 00010 00000 00000101000
	 *	=0x8FC20028
	 * 
	 *	=800000B4 nop # nop
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=800000B8 addiu $2,$2,-2 # $2 <- $2 + -2
	 *	=00100100010000101111111111111110
	 *	=0x2442FFFE
	 * 
	 *	=800000BC move $4,$2 # $4 <- $2 + $0 [add $4,$2,$0]
	 *	=000000 00010 00000 00100 00000100000
	 *	=0x00402020
	 * 
	 *	=800000C0 jal 80000040 # link and goto getFibonacci
	 *	=00001100000000000000000001000000
	 *	=0x0C000040
	 * 
	 *	=800000C4 nop # nop - delay slot
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	=800000C8 addu $16,$16,$2 # $16 <- $16 + 2
	 *	=0000010000000101000000000100001
	 *	=0x02028021
	 * 
	 *	=800000CC sw $16,16($fp) # memory($fp + 16) <- $16
	 *	=101011 11101 10000 0000000000010000
	 *	=0xAFB00010
	 * 
	 *	=#$L4: 
	 *	=800000D0 lw $2,16($fp) # $2 <- memory($fp + 16)
	 *	=100011 11110 00010 00000 00000010000
	 *	=0x8FC20010
	 * 
	 *	=800000D4 move $29,$30 # $sp <- $fp + $0 [add $29,$30,$0]
	 *	=00000011110000001110100000100000
	 *	=0x03C0E820
	 * 
	 *	=800000D8 lw $31,32($sp) # $31 <- memory($29 + 32)
	 *	=10001111101111110000000000100000
	 *	=0x8FBF0020
	 * 
	 *	=800000DC lw $fp,28($sp) # $30 <- memory($29 + 28)
	 *	=10001111101111100000000000011100
	 *	=0x8FBE001C
	 * 
	 *	=800000E0 lw $16,24($sp) # $16 <- memory($29 + 24)
	 *	=10001111101100000000000000011000
	 *	=0x8FB00018
	 * 
	 *	=800000E4 addiu $sp,$sp,40 # $29 <- $29 + 40
	 *	=00100111101111010000000000101000
	 *	=0x27BD0028
	 * 
	 *	=800000E8 jr $31 # goto $31
	 *	=00000011111000000000000000001000
	 *	=0x03E00008
	 * 
	 *	=800000EC nop # nop - delay slot
	 *	=000000 00000 00000 00000 00000100000
	 *	=0x00000020
	 * 
	 *	# $4 == n # $2 == fib(n)
	 */
}
